
window.onload = function () {
    /* 头部js */
    heardShow();
    //导航栏定位,宽度>800 高度>189.6
    $(window).scroll(function (event) {
        if ($(window).width() > 800 && $(window).scrollTop() > 190) {
            $('.webHeader_nav2_ul').css({ 'position': 'fixed', 'top': '0px', 'background-color': '#fff', 'z-index': '2' });
            $('.webHeader_nav2_ul li').css('margin', '3rem 2rem 2rem')
        } else {
            $('.webHeader_nav2_ul').css({ 'position': 'unset', 'background-color': 'transparent' });
            $('.webHeader_nav2_ul li').css('margin', '0 4rem')
        }
    });
    //接受参数
    GetRequest();
    //点击查看详情
    goodsClick($("#hotgoodsClick li"));
    goodsClick($("#otherGoodsDiv figure"));
    /* 点击删除节点 */
    deleteNode($('.cartUl'));
    //初始化购物车数量
    allNumberCountent();
}



var arrayGoodsShow=[
    {
        imgd:'../img/order/hh1.jpg',
        imgx:'../img/order/hhx1.jpg'
    },
    {
        imgd:'../img/order/hh2.jpg',
        imgx:'../img/order/hhx2.jpg'
    },
    {
        imgd:'../img/order/hh3.jpg',
        imgx:'../img/order/hhx3.jpg'
    },
    {
        imgd:'../img/order/hh4.jpg',
        imgx:'../img/order/hhx4.jpg'
    },
    {
        imgd:'../img/order/hh5.jpg',
        imgx:'../img/order/hhx5.jpg'
    },
    {
        imgd:'../img/order/hh6.jpg',
        imgx:'../img/order/hhx6.jpg'
    },
    {
        imgd:'../img/order/hh7.jpg',
        imgx:'../img/order/hhx7.jpg'
    },
    {
        imgd:'../img/order/hh8.jpg',
        imgx:'../img/order/hhx8.jpg'
    },
];

//接受request
var theRequest = new Object();
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
        }
    };
    //获取主图片
    var imgSrc = arrayGoodsShow[theRequest.id-1];
    $(".titles").html("植物花卉00"+theRequest.id);
    // 主板块
    $(".leftGoodsImg").attr('src',imgSrc.imgd);
    $(".imgShow").attr('src',imgSrc.imgd);
    $(".leftGoodsH6").html("植物花卉00"+theRequest.id);
    $(".leftGoodsH6x").attr('src',imgSrc.imgx);


    //随机生成4个不重复的数字，除了主要页面
    var array = [];
    var length = array.length
    while(length < 4){
        var value = Math.floor(Math.random()*8);
        if(theRequest.id-1 !== value && array.indexOf(value)===-1){
            length = array.push(value);
        }
    };
    //插入数据
    for(i=0;i<4;i++){
        $(".leftGoodsaboutimg").eq(i).attr('src',arrayGoodsShow[array[i]].imgd);
        var count = array[i]+1;
        $(".eftGoodsaboutH6").eq(i).html("植物花卉00"+count);
    };

    //右侧数据
    var j = 0;
    for(i = 0;i<8;i++){
        if(i == theRequest.id-1){
            j = i;
            continue;
        };
        $(".rigthGoodsImg").eq(j).attr('src',arrayGoodsShow[i].imgx);
        var count = i+1;
        $(".rigthGoodsH6").eq(j).html("植物花卉00"+count);
        j++;
    }
}


//购物车计数器
var number = 1;

//减法
function subtract(){
    if(number <= 1){
        number = 1;
        $(".counterSpanInput").val(number)
        return;
    };
    number --;
    $(".counterSpanInput").val(number)
}

//加法
function add(){
    number ++;
    $(".counterSpanInput").val(number)
}

//input 失去焦点
function getValue(){
    var getValueNubmer = $('.counterSpanInput').val();
    if(getValueNubmer>=1){
        number = getValueNubmer;
        $(".counterSpanInput").val(number)
    }else{
        number = 1;
        $(".counterSpanInput").val(number)
    }
}

function addCart(){
    //数组为空 就添加
    same:
    if(carArray.length == 0){
        addArray();
    }else{
        //判断数组里面是否存在同一个，如果没有
        for(i=0 ;i<carArray.length;i++){
            //计算数量
            if(carArray[i].id === theRequest.id){
               carArray[i].number += number;
                break same;
            }
        };
        addArray();
    };
    //存入
    window.localStorage.removeItem('carArray');
    window.localStorage.setItem('carArray',JSON.stringify(carArray));

    //添加节点
    addNode($('.sidebar'),$('.cartUl'));
}

function addArray(){
    //添加对象，如果有
    var carObj = {
        "img":arrayGoodsShow[theRequest.id-1].imgx,
        "number":number,
        "name":'植物花卉00'+theRequest.id,
        "id":theRequest.id,
    };
    //添加数组
    carArray.push(carObj);
}



$('.deleteC').click(function(){
    $(".sidebar").stop(true,false).hide(500);
})
